/** CODE EVAL::Easy [Penultiate Word]
 *	Write a program which finds the next-to-last word in a string.
 *
 *	Solution by Michael Pio Arroyo
 */

#include <iostream>
#include <fstream>
#include <string>
#include <vector>

void MakeWordList(std::string a_line, std::vector<std::string> &a_list);
void FindNTL(std::vector<std::string> a_list);

int main(int argc, char** argv) {
	std::ifstream file;
	std::string line;
	file.open(argv[1]);
	std::vector<std::string> list;
	while(getline(file, line)) {
		MakeWordList(line, list);
		FindNTL(list);
	}

	file.close();
	return 0;
}

/**
 *	Create list of words
 *	@param a_line...string of words
 *	@param a_line...output vector word list
 */
void MakeWordList(std::string a_line, std::vector<std::string> &a_list) {
	std::string t;
	for(std::string::iterator it=a_line.begin(); it!=a_line.end(); ++it) {
		// take words from line and add to list
		if(*it != ' ') {
			t.push_back(*it);
		} else {
			a_list.push_back(t);
			t.clear();
		}
	}
	// get the last word
	a_list.push_back(t);
	t.clear();
}

/**
 *	Find next-to-last word
 *	Print it
 *	@param a_list...vector word list
 */
void FindNTL(std::vector<std::string> a_list) {
	size_t len = a_list.size();
	std::cout << a_list.at(len-2) << '\n';
}